package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO 购物车DTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前添加的菜品或套餐是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long id = BaseContext.getCurrentId();
        shoppingCart.setUserId(id);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        //存在直接数量加一
        if (shoppingCarts != null && shoppingCarts.size()>0){
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);
            int number = shoppingCart1.getNumber() + 1 ;
            shoppingCart1.setNumber(number);
            shoppingCartMapper.update(shoppingCart1);
        }else {
            //不存在添加菜品或套餐
            Long dishId = shoppingCart.getDishId();
            if (dishId != null){
                Dish dish = dishMapper.getStagtusById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else {
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getsetmealById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getshoppingCart() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.getShoppingCart(userId);
    }

    /**
     * 删除购物车中一条数据
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //根据传进来的dto对象获取数据库ID
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
       List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
       ShoppingCart shoppingCart1 = shoppingCarts.get(0);
       int number = shoppingCart1.getNumber();
        //判断数量 如果数量大于1 则数量-1
        if (number > 1){
            shoppingCart1.setNumber(shoppingCart1.getNumber() -1);
            shoppingCartMapper.update(shoppingCart1);
        }else {
            //数量等于1 删除这条记录
            shoppingCartMapper.del(shoppingCart1);
        }

    }

    /**
     *  清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean();
    }
}
